#!/usr/bin/env python
# @Date    : 2022-05-06
# @Author  : Bright (brt2@qq.com)
# @Link    : https://gitee.com/brt2
# @Version : 0.1.2

import os

_g = {
    "DIR_CONF": None,  # "~/.local/app_xxxx/"
}

def rpath(from_, relative_path):
    return os.path.join(os.path.dirname(from_), relative_path)

def get_app_name_by_filename(__file__):
    filename, _ = os.path.splitext(os.path.basename(__file__))
    return filename

def get_app_name_by_foldername(__file__):
    """ 获取__file__文件所在文件夹的string """
    dir_ = os.path.dirname(__file__)
    if not dir_:
        dir_ = os.path.dirname(os.path.abspath(__file__))
    return os.path.basename(dir_)

get_app_name = get_app_name_by_foldername

def get_conf_dir(app_name):
    if not _g.get("DIR_CONF"):
        dir_conf = os.path.join(os.path.expanduser("~/.local/"), app_name)
        if not os.path.exists(dir_conf):
            os.makedirs(dir_conf)
        _g["DIR_CONF"] = dir_conf
    return _g["DIR_CONF"]
